local shishi = fk.CreateSkill{
  name = "hanqing__shishi",
  dynamic_desc = function (self, player, lang)
    local rate1, rate2, rate3 = "1：2", "2：3", "3：4"
    if player:getMark("hanqing__shishi_basic") > 0 then
      rate1 = "2：1"
    end
    if player:getMark("hanqing__shishi_trick") > 0 then
      rate2 = "3：2"
    end
    if player:getMark("hanqing__shishi_equip") > 0 then
      rate3 = "4：3"
    end
    return "hanqing__shishi_inner:"..rate1..":"..rate2..":"..rate3
  end,
}

Fk:loadTranslationTable{
  ["hanqing__shishi"] = "市势",
  [":hanqing__shishi"] = "出牌阶段限两次，你可以按以下规则与其他角色交换牌，然后展示你交出的牌，你可以重铸任意张因此获得的牌：<br>"..
  "①基本牌→1：2；<br>②锦囊牌→2：3；<br>③装备牌→3：4。",

  [":hanqing__shishi_inner"] = "出牌阶段限两次，你可以按以下规则与其他角色交换牌，然后展示你交出的牌，你可以重铸任意张因此获得的牌：<br>"..
  "①基本牌→{1}；<br>②锦囊牌→{2}；<br>③装备牌→{3}。",

  ["#hanqing__shishi"] = "市势：你可以将一种类别的牌按一定比例与其他角色交换",
  ["#hanqing__shishi-basic"] = "市势：用%arg张基本牌交换%arg2张牌",
  ["#hanqing__shishi-trick"] = "市势：用%arg张锦囊牌交换%arg2张牌",
  ["#hanqing__shishi-equip"] = "市势：用%arg张装备牌交换%arg2张牌",
  ["#hanqing__shishi-exchange"] = "市势：选择 %dest 的%arg张牌",
  ["#hanqing__shishi-recast"] = "市势：你可以重铸任意张获得的牌",
}

shishi:addEffect("active", {
  anim_type = "control",
  prompt = function (self, player, selected_cards)
    if #selected_cards == 0 then
      return "#hanqing__shishi"
    else
      local type = Fk:getCardById(selected_cards[1]):getTypeString()
      local rate
      if type == "basic" then
        rate = player:getMark("hanqing__shishi_"..type) > 0 and "2:1" or "1:2"
      elseif type == "trick" then
        rate = player:getMark("hanqing__shishi_"..type) > 0 and "3:2" or "2:3"
      elseif type == "equip" then
        rate = player:getMark("hanqing__shishi_"..type) > 0 and "4:3" or "3:4"
      end
        return "#hanqing__shishi-"..type..":::"..rate
    end
  end,
  min_card_num = 1,
  max_card_num = 4,
  target_num = 1,
  can_use = function (self, player)
    return player:usedSkillTimes(shishi.name, Player.HistoryPhase) < 2
  end,
  card_filter = function (self, player, to_select, selected)
    if #selected == 0 then
      return true
    elseif Fk:getCardById(selected[1]).type == Fk:getCardById(to_select).type then
      local type = Fk:getCardById(selected[1]):getTypeString()
      if type == "basic" then
        return #selected < (player:getMark("hanqing__shishi_"..type) > 0 and 2 or 1)
      elseif type == "trick" then
        return #selected < (player:getMark("hanqing__shishi_"..type) > 0 and 3 or 2)
      elseif type == "equip" then
        return #selected < (player:getMark("hanqing__shishi_"..type) > 0 and 4 or 3)
      end
    end
  end,
  target_filter = function (self, player, to_select, selected, selected_cards)
    if #selected == 0 and to_select ~= player and #selected_cards > 0 then
      local type = Fk:getCardById(selected_cards[1]):getTypeString()
      if type == "basic" then
        return #selected_cards == (player:getMark("hanqing__shishi_"..type) > 0 and 2 or 1) and
          #to_select:getCardIds("he") >= (player:getMark("hanqing__shishi_"..type) > 0 and 1 or 2)
      elseif type == "trick" then
        return #selected_cards == (player:getMark("hanqing__shishi_"..type) > 0 and 3 or 2) and
          #to_select:getCardIds("he") >= (player:getMark("hanqing__shishi_"..type) > 0 and 2 or 3)
      elseif type == "equip" then
        return #selected_cards == (player:getMark("hanqing__shishi_"..type) > 0 and 4 or 3) and
          #to_select:getCardIds("he") >= (player:getMark("hanqing__shishi_"..type) > 0 and 3 or 4)
      end
    end
  end,
  feasible = function (self, player, selected, selected_cards, card)
    return #selected_cards > 0 and #selected == 1
  end,
  on_use = function(self, room, effect)
    local player = effect.from
    local target = effect.tos[1]
    local n = 0
    local type = Fk:getCardById(effect.cards[1]):getTypeString()
    if type == "basic" then
      n = player:getMark("hanqing__shishi_"..type) > 0 and 1 or 2
    elseif type == "trick" then
      n = player:getMark("hanqing__shishi_"..type) > 0 and 2 or 3
    elseif type == "equip" then
      n = player:getMark("hanqing__shishi_"..type) > 0 and 3 or 4
    end
    local cards = room:askToChooseCards(player, {
      target = target,
      min = n,
      max = n,
      flag = "he",
      skill_name = shishi.name,
      prompt = "#hanqing__shishi-exchange::"..target.id..":"..n,
    })
    room:swapCards(player, {
      {player, effect.cards},
      {target, cards},
    }, shishi.name)
    local show = table.filter(effect.cards, function (id)
      return table.contains(target:getCardIds("h"), id)
    end)
    if #show > 0 then
      room:showCards(show)
    end
    if player.dead then return end
    cards = table.filter(cards, function (id)
      return table.contains(player:getCardIds("h"), id)
    end)
    if #cards > 0 then
      cards = room:askToCards(player, {
        min_num = 1,
        max_num = #cards,
        include_equip = false,
        skill_name = shishi.name,
        pattern = tostring(Exppattern{ id = cards }),
        prompt = "#hanqing__shishi-recast",
        cancelable = true,
      })
      if #cards > 0 then
        room:recastCard(cards, player, shishi.name)
      end
    end
  end,
})

shishi:addLoseEffect(function (self, player, is_death)
  for _, type in ipairs({"basic", "trick", "equip"}) do
    player.room:setPlayerMark(player, "hanqing__shishi_"..type, 0)
  end
end)

return shishi
